package manager;

import entity.InsertItem;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sessionBean.ShortQSessionBean;

@ManagedBean(name = "questionBean")
@ViewScoped
public class shortQuestionManagerBean implements Serializable {

    @EJB
    private ShortQSessionBean questionSession;
    private int qid;
    private String question;
    private String type;
    private InsertItem selectedQ;
    private List<InsertItem> qItems;

    @PostConstruct
    public void init() {
        qItems = questionSession.getAllQuestions();
    }

    public void clearValue() {
        qid = 0;
        question = null;
        selectedQ = null;
        qItems = null;
    }

    public void updateValue() {
        qid = selectedQ.getId();
        question = selectedQ.getQuestion();
        type = selectedQ.getType();
    }

    public void addQuestion() {
        if (questionSession.addQuestion(qid, question, type)) {
            System.out.println("Successfully created a question ");
        } else {
            System.err.println("Error occur");
        }
    }

    public void editQuesiton() {
        
        if (questionSession.editQuestion(qid, question, type)) {
            System.out.println("Successfully edit a question ");
        } else {
            System.err.println("Error occur");
        }
    }

    public void deleteQuestion() {
        if (questionSession.deleteQuestion(qid)) {
            System.out.println("Successfully delete a question ");
        } else {
            System.err.println("Error occur");
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getQid() {
        return qid;
    }

    public void setQid(int qid) {
        this.qid = qid;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public InsertItem getSelectedQ() {
        return selectedQ;
    }

    public void setSelectedQ(InsertItem selectedQ) {
        this.selectedQ = selectedQ;
    }

    public List<InsertItem> getqItems() {
        return qItems;
    }

    public void setqItems(List<InsertItem> qItems) {
        this.qItems = qItems;
    }
}
